{% set template = haproxy_template.vmware_psc %}
# VMware PSC Defaults
defaults {{ name }}
	# Default to TCP mode
	mode tcp

	# Retry another server on connection failure
	option redispatch

	# Timeouts
	timeout connect            5s
	timeout http-keep-alive    1s
	timeout http-request       15s
	timeout queue              30s
	timeout tarpit             1m
	timeout client             300s
	timeout server             300s

	# Logging options
	option tcplog
	option dontlognull
	log global

	# Set default balancing algorithm
	balance roundrobin

	# Default server check options
{% if proxy.defaults is defined %}
	default-server inter {{ proxy.defaults.interval|default(template.interval) }} rise {{ proxy.defaults.rise|default(template.rise) }} fall {{ proxy.defaults.fall|default(template.fall) }}
{% else %}
	default-server inter {{ template.interval }} rise {{ template.rise }} fall {{ template.fall }}
{% endif %}

{% if proxy.peers is defined %}
# Peers to sync session affinity between
peers peers_{{ name }}
{% for peer in proxy.peers %}
	peer {{ peer.name }} {{ peer.ip }}:{{ peer.port }}
{% endfor %}
{% endif %}

# VMware PSC Front-End
frontend fe_{{ name }}_https
	mode http

	# Listen on HTTPS port
{% for bind in proxy.frontend.bind %}
{% for port in template.port_list if port == 443 %}
	bind {{ bind.ip }}:{{ port }} ssl crt {{ bind.crt }}
{% endfor %}
{% endfor %}

	# Use PSC HTTPS Back-End
	use_backend be_{{ name }}_https


frontend fe_{{ name }}_tcp
	# Listen on TCP ports
{% for bind in proxy.frontend.bind %}
{% for port in template.port_list if port != 443 %}
	bind {{ bind.ip }}:{{ port }}
{% endfor %}
{% endfor %}

	# Use PSC TCP Back-End
	use_backend be_{{ name }}_tcp

# PSC Back-Ends

backend be_{{ name }}_https
	mode http

	# Health check options
	option httpchk GET\ /websso/HealthStatus\ HTTP/1.1\r\nHost:\ {{ proxy.frontend.hostname|default("psc") }}\r\nConnection: Close\r\n\r\n
	option log-health-checks
	http-check expect status 200

	# Maintain affniity based on source address
{% if proxy.peers is defined %}
	stick-table type ip size 1k expire {{ proxy.backend.session_expiry|default("8h") }} peers peers_{{ name }}
{% elif haproxy_peers is defined %}
	stick-table type ip size 1k expire {{ proxy.backend.session_expiry|default("8h") }} peers peers_global
{% else %}
	stick-table type ip size 1k expire {{ proxy.backend.session_expiry|default("8h") }}
{% endif %}
	stick on src

# PSC servers
{% for server in proxy.backend.server %}
	server {{ server.name }} {{ server.ip }}:443 check ssl ca-file {{ server.ca_file }}
{% endfor %}

backend be_{{ name }}_tcp
	# Health check options
	option httpchk GET\ /websso/HealthStatus\ HTTP/1.1\r\nHost:\ {{ proxy.frontend.hostname|default("psc") }}\r\nConnection: Close\r\n\r\n
	option log-health-checks
	http-check expect status 200

	# Maintain affinity based on source address
	stick on src table be_{{ name }}_https

	# PSC servers
{% for server in proxy.backend.server %}
	server {{ server.name }} {{ server.ip }} check port 443 check-ssl ca-file {{ server.ca_file }}
{% endfor %}
